﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;

namespace NHINDirect
{
    public class MemoryX509Store : CertificateStore
    {
        X509Certificate2Collection m_certs;
        
        public MemoryX509Store()
            : base()
        {
            this.m_certs = new X509Certificate2Collection();
        }
        
        public MemoryX509Store(string filePath, string password, X509KeyStorageFlags flags)
            : this()
        {
            this.ImportKeyFile(filePath, password, flags);
        }
        
        public override X509Certificate2Collection this[string subjectName]
        {
            get 
            { 
                return this.m_certs.Find(X509FindType.FindBySubjectName, subjectName, false);
            }
        }

        public override bool Contains(X509Certificate2 cert)
        {
            return this.m_certs.Contains(cert);
        }

        public override void Add(X509Certificate2 cert)
        {
            lock(this.m_certs)
            {
                this.ValidateCriteria(cert);
                this.m_certs.Add(cert);
            }
        }
                
        public override void Remove(X509Certificate2 cert)
        {
            lock(this.m_certs)
            {
                this.m_certs.Remove(cert);
            }
        }

        public override IEnumerable<X509Certificate2> GetCertificates()
        {
            X509Certificate2Collection certs;
            lock(this.m_certs)
            {
                certs = new X509Certificate2Collection(this.m_certs);
            }
            return certs.Enumerate();
        }
    }
}
